package app.sb.jly.com.ji_liang_yuan.activitys.ac;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.kongzue.dialog.interfaces.OnMenuItemClickListener;
import com.kongzue.dialog.util.TextInfo;
import com.kongzue.dialog.v3.BottomMenu;

import app.sb.jly.com.ji_liang_yuan.R;
import app.sb.jly.com.ji_liang_yuan.activitys.application.BaseApplication;
import app.sb.jly.com.ji_liang_yuan.activitys.commons.Constants;
import app.sb.jly.com.ji_liang_yuan.activitys.commons.PDALogUtils;
import app.sb.jly.com.ji_liang_yuan.activitys.commons.UtilsLog;
import app.sb.jly.com.ji_liang_yuan.activitys.model.CheckDataModel;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//主界面
public class HomeActivity extends BaseActivity {

    String type = "";
    String carModel = "";
    int zhoushu = 0;
    int result = 0;
    @BindView(R.id.ac_home_zhizao_danwei)
    EditText acHomeZhizaoDanwei;
    @BindView(R.id.ac_home_iv_r_1)
    ImageView acHomeIvR1;
    @BindView(R.id.ac_home_jianding_didian)
    EditText acHomeJiandingDidian;
    @BindView(R.id.ac_home_iv_r_2)
    ImageView acHomeIvR2;
    @BindView(R.id.ac_home_left)
    TextView acHomeLeft;
    @BindView(R.id.ac_home_middle)
    TextView acHomeMiddle;
    @BindView(R.id.ac_home_middle2)
    TextView acHomeMiddle2;
    @BindView(R.id.ac_home_right)
    TextView acHomeRight;

    public LocationClient mLocationClient = null;
    @BindView(R.id.ac_home_layout)
    LinearLayout acHomeLayout;
    @BindView(R.id.ac_home_carModel_txt)
    TextView acHomeCarModelTxt;
    @BindView(R.id.ac_home_et_carModel)
    EditText acHomeEtCarModel;
    @BindView(R.id.ac_home_carModel_iv)
    ImageView acHomeCarModelIv;
    @BindView(R.id.ac_home_zhouShu_txt)
    TextView acHomeZhouShuTxt;
    @BindView(R.id.ac_home_et_zhouShu)
    EditText acHomeEtZhouShu;
    @BindView(R.id.ac_home_zhouShu_iv)
    ImageView acHomeZhouShuIv;
    @BindView(R.id.ac_home_result_txt)
    TextView acHomeResultTxt;
    @BindView(R.id.ac_home_et_result)
    EditText acHomeEtResult;
    @BindView(R.id.ac_home_result_iv)
    ImageView acHomeResultIv;
    @BindView(R.id.ac_home_data_layout)
    LinearLayout acHomeDataLayout;
    private MyLocationListener myListener = new MyLocationListener();
    boolean isFirst = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        setTopTitle("主界面");
//        setBackBtn();
        type = getIntent().getStringExtra("type");
        carModel = getIntent().getStringExtra("carModel");
        zhoushu = getIntent().getIntExtra("zhoushu", 0);
        result = getIntent().getIntExtra("result", 0);
        BaseApplication.myCheckDataModel.setType0(carModel);
        UtilsLog.e("type=======" + type);
        UtilsLog.e("carModel=======" + carModel);
        UtilsLog.e("zhoushu=======" + zhoushu);
        UtilsLog.e("result=======" + result);
//        if (getIntent().hasExtra("title")) {
//            setTopTitle("参考车辆信息");
//            acHomeLayout.setVisibility(View.GONE);
//            acHomeDataLayout.setVisibility(View.VISIBLE);
////            acHomeMiddle.setVisibility(View.GONE);
//        }
        setLeftTxt("返回", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mLocationClient = new LocationClient(getApplicationContext());
        //声明LocationClient类

        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
        baiduMap();

        mLocationClient.start();


        BaseApplication.myCheckDataModel = new CheckDataModel();

        acHomeZhizaoDanwei.setText(Constants.zhi_zao_dan_wei[0]);
    }

    void baiduMap() {
        LocationClientOption option = new LocationClientOption();
        option.setIsNeedAddress(true);
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
//可选，设置定位模式，默认高精度
//LocationMode.Hight_Accuracy：高精度；
//LocationMode. Battery_Saving：低功耗；
//LocationMode. Device_Sensors：仅使用设备；
//LocationMode.Fuzzy_Locating, 模糊定位模式；v9.2.8版本开始支持，可以降低API的调用频率，但同时也会降低定位精度；

        option.setCoorType("bd09ll");
//可选，设置返回经纬度坐标类型，默认GCJ02
//GCJ02：国测局坐标；
//BD09ll：百度经纬度坐标；
//BD09：百度墨卡托坐标；
//海外地区定位，无需设置坐标类型，统一返回WGS84类型坐标

//        option.setFirstLocType(FirstLocType.SPEED_IN_FIRST_LOC);
//可选，首次定位时可以选择定位的返回是准确性优先还是速度优先，默认为速度优先
//可以搭配setOnceLocation(Boolean isOnceLocation)单次定位接口使用，当设置为单次定位时，setFirstLocType接口中设置的类型即为单次定位使用的类型
//FirstLocType.SPEED_IN_FIRST_LOC:速度优先，首次定位时会降低定位准确性，提升定位速度；
//FirstLocType.ACCUARACY_IN_FIRST_LOC:准确性优先，首次定位时会降低速度，提升定位准确性；

        option.setScanSpan(1000);
//可选，设置发起定位请求的间隔，int类型，单位ms
//如果设置为0，则代表单次定位，即仅定位一次，默认为0
//如果设置非0，需设置1000ms以上才有效

        option.setOpenGps(true);
//可选，设置是否使用卫星定位，默认false
//使用高精度和仅用设备两种定位模式的，参数必须设置为true

        option.setLocationNotify(true);
//可选，设置是否当卫星定位有效时按照1S/1次频率输出卫星定位结果，默认false

        option.setIgnoreKillProcess(false);
//可选，定位SDK内部是一个service，并放到了独立进程。
//设置是否在stop的时候杀死这个进程，默认（建议）不杀死，即setIgnoreKillProcess(true)

        option.SetIgnoreCacheException(false);
//可选，设置是否收集Crash信息，默认收集，即参数为false

        option.setWifiCacheTimeOut(5 * 60 * 1000);
//可选，V7.2版本新增能力
//如果设置了该接口，首次启动定位时，会先判断当前Wi-Fi是否超出有效期，若超出有效期，会先重新扫描Wi-Fi，然后定位

        option.setEnableSimulateGps(false);
//可选，设置是否需要过滤卫星定位仿真结果，默认需要，即参数为false

        option.setNeedNewVersionRgc(true);
//可选，设置是否需要最新版本的地址信息。默认需要，即参数为true

        mLocationClient.setLocOption(option);
//mLocationClient为第二步初始化过的LocationClient对象
//需将配置好的LocationClientOption对象，通过setLocOption方法传递给LocationClient对象使用
//更多LocationClientOption的配置，请参照类参考中LocationClientOption类的详细说明
    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取经纬度相关（常用）的结果信息
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明

            double latitude = location.getLatitude();    //获取纬度信息
            double longitude = location.getLongitude();    //获取经度信息
            float radius = location.getRadius();    //获取定位精度，默认值为0.0f
//
//            UtilsLog.e("纬度===" + latitude + "");
//            UtilsLog.e("经度===" + longitude + "");

            String coorType = location.getCoorType();
            //获取经纬度坐标类型，以LocationClientOption中设置过的坐标类型为准

            int errorCode = location.getLocType();
            //获取定位类型、定位错误返回码，具体信息可参照类参考中BDLocation类中的说明
//            String addrstr = location.getAddrStr();
//            String Address = location.getAddress().country + "-" + location.getAddress().province
//                    + "-" + location.getAddress().city
//                    + "-" + location.getAddress().district;
//            String myAddress = location.getAddress().address;
//            UtilsLog.e("errorCode==" + errorCode);
////            UtilsLog.e("addrstr==" + addrstr);
////            UtilsLog.e("Address==" + Address);
//            UtilsLog.e("getCountry==" + location.getCountry());
//            UtilsLog.e("getProvince==" + location.getProvince());
//            UtilsLog.e("getCity==" + location.getCity());
//            UtilsLog.e("getDistrict==" + location.getDistrict());
//            UtilsLog.e("getStreet==" + location.getStreet());
//            UtilsLog.e("getStreetNumber==" + location.getStreetNumber());
            if (isFirst == false) {
                if (!TextUtils.isEmpty(location.getCity()) && !TextUtils.isEmpty(location.getDistrict())
                        && !TextUtils.isEmpty(location.getStreet()) && !TextUtils.isEmpty(location.getStreetNumber())
                        ) {
                    acHomeJiandingDidian.setText(location.getCity() + location.getDistrict() +
                            location.getStreet() + location.getStreetNumber());
//                    BaseApplication.myCheckDataModel.setJiandingdidian(acHomeJiandingDidian.getText().toString().trim());
                    isFirst = true;
                }
            }
        }
    }

    @OnClick({R.id.ac_home_iv_r_1, R.id.ac_home_iv_r_2,
            R.id.ac_home_left, R.id.ac_home_middle,
            R.id.ac_home_right, R.id.ac_home_middle2, R.id.ac_home_carModel_iv, R.id.ac_home_zhouShu_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_home_iv_r_1:
                showMyDialog(Constants.zhi_zao_dan_wei, R.id.ac_home_zhizao_danwei);
                break;
            case R.id.ac_home_middle2:
                //多轴刚性和铰接挂车
                BaseApplication.myCheckDataModel.setJiandingdidian(acHomeJiandingDidian.getText().toString().trim());
                Intent intent = new Intent(this, JiZhongCaiJiActivity.class);
                intent.putExtra("in_type", 3);
                intent.putExtra("type", type);
                intent.putExtra("carModel", carModel);
                intent.putExtra("zhoushu", zhoushu);
                intent.putExtra("result", result);
                intent.putExtra("zhizaodanwei", acHomeZhizaoDanwei.getText().toString().trim());
                startActivity(intent);
                break;
            case R.id.ac_home_iv_r_2:
//                showMyDialog(Constants.,R.id.ac_home_jianding_didian);
                if (mLocationClient != null) {
                    mLocationClient.stop();
                }
                isFirst = false;
                mLocationClient.restart();
                acHomeJiandingDidian.setText("");
                break;
            case R.id.ac_home_left:
                BaseApplication.myCheckDataModel.setJiandingdidian(acHomeJiandingDidian.getText().toString().trim());
                Intent intenta = new Intent(this, JiZhongCaiJiActivity.class);
                intenta.putExtra("in_type", 1);
                intenta.putExtra("type", type);
                intenta.putExtra("carModel", carModel);
                intenta.putExtra("zhoushu", zhoushu);
                intenta.putExtra("result", result);
                intenta.putExtra("zhizaodanwei", acHomeZhizaoDanwei.getText().toString().trim());
                startActivity(intenta);
                break;
            case R.id.ac_home_middle:
                //生成报表
                UtilsLog.e("111111111111");
//        setTxtContents("济南");
                if (BaseApplication.myCheckDataModel != null) {
                    setTxtContents(BaseApplication.myCheckDataModel.getTxt_name());
                }

                UtilsLog.e("22222222222");
                break;
            case R.id.ac_home_right:
                BaseApplication.myCheckDataModel.setJiandingdidian(acHomeJiandingDidian.getText().toString().trim());
                Intent intent2 = new Intent(this, JiZhongCaiJiActivity.class);
                intent2.putExtra("in_type", 2);
                intent2.putExtra("type", type);
                intent2.putExtra("carModel", carModel);
                intent2.putExtra("zhoushu", zhoushu);
                intent2.putExtra("result", result);
                intent2.putExtra("zhizaodanwei", acHomeZhizaoDanwei.getText().toString().trim());
                startActivity(intent2);
                break;
            case R.id.ac_home_carModel_iv:
                showMyDialog3(Constants.home_chexing, R.id.ac_home_carModel_iv);
                break;
            case R.id.ac_home_zhouShu_iv:
                showMyDialog3(Constants.home_zhoushu, R.id.ac_home_zhouShu_iv);
                break;
        }
    }

    private void showMyDialog3(String[] str, final int id) {
        BottomMenu.show(this, str, new OnMenuItemClickListener() {
            @Override
            public void onClick(String text, int index) {
                //返回参数 text 即菜单名称，index 即菜单索引
                UtilsLog.e("---showMyDialog=333=" + text + "===" + index);
                switch (id) {
                    case R.id.ac_home_carModel_iv:
                        acHomeEtCarModel.setText(text);
//                        SharedPreferencesUtils.setHomeCarModel(HomeActivity.this,text);
                        BaseApplication.myCheckDataModel.setType0(text);
                        break;
                    case R.id.ac_home_zhouShu_iv:
                        acHomeEtZhouShu.setText(text);
//                        SharedPreferencesUtils.setHomeZhoushu(HomeActivity.this,text);
                        BaseApplication.myCheckDataModel.setType0(text);
                        break;
                }
            }
        }).setMenuTextInfo(new TextInfo().setBold(true).setFontColor(Color.BLACK).setGravity(Gravity.CENTER));
    }

    void setTxtContents(String fileName) {
        String str = "";
//        for (int i = 0; i < Constants.contents.length; i++) {
//            str += Constants.contents[i] + "\r\n";
//        }
//        UtilsLog.e(str);

        if (BaseApplication.myCheckDataModel == null) {
            return;
        }
        //多轴刚性和铰接挂车
        if (BaseApplication.myCheckDataModel.getType0().equals("多轴刚性")
                || BaseApplication.myCheckDataModel.getType0().equals("铰接挂车")) {
            str = BaseApplication.myCheckDataModel.getSongjiandaiwei() + "\r\n"
                    + BaseApplication.myCheckDataModel.getJiandingdidian() + "\r\n"
                    + BaseApplication.myCheckDataModel.getWendu() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhidaodanwei() + "\r\n"
                    + BaseApplication.myCheckDataModel.getJiliangqijumingcheng() + "\r\n"
                    + BaseApplication.myCheckDataModel.getXinhaoguige() + "\r\n"
                    + BaseApplication.myCheckDataModel.getFenduzhi() + "\r\n"

                    + BaseApplication.myCheckDataModel.getZuidachengliang() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZuixiaochengliang() + "\r\n"

                    + BaseApplication.myCheckDataModel.getDengji() + "\r\n"

                    + BaseApplication.myCheckDataModel.getZuigaosudu() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZuidisudu() + "\r\n"

                    + BaseApplication.myCheckDataModel.getShijian() + "\r\n"

                    + BaseApplication.myCheckDataModel.getShijian() + "\r\n"

                    + BaseApplication.myCheckDataModel.getType0() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhoushu0() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhongliang0() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisuzhongxin00() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisuzhongxin01() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhongsuzhognxin00() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhongsuzhognxin01() + "\r\n"
                    + BaseApplication.myCheckDataModel.getGaosuzhognxin00() + "\r\n"
                    + BaseApplication.myCheckDataModel.getGaosuzhognxin01() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisupianzuo00() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisupianzuo01() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisupianyou00() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisupianyou01() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZuidawucha1() + "\r\n"
                    + BaseApplication.myCheckDataModel.getMPE1() + "\r\n"
                    + BaseApplication.myCheckDataModel.getHege1() + "\r\n"
                    + BaseApplication.myCheckDataModel.getUname() + "\r\n"
            ;
        } else {
            str = BaseApplication.myCheckDataModel.getSongjiandaiwei() + "\r\n"
                    + BaseApplication.myCheckDataModel.getJiandingdidian() + "\r\n"
                    + BaseApplication.myCheckDataModel.getWendu() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhidaodanwei() + "\r\n"
                    + BaseApplication.myCheckDataModel.getJiliangqijumingcheng() + "\r\n"
                    + BaseApplication.myCheckDataModel.getXinhaoguige() + "\r\n"
                    + BaseApplication.myCheckDataModel.getFenduzhi() + "\r\n"

                    + BaseApplication.myCheckDataModel.getZuidachengliang() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZuixiaochengliang() + "\r\n"

                    + BaseApplication.myCheckDataModel.getDengji() + "\r\n"

                    + BaseApplication.myCheckDataModel.getZuigaosudu() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZuidisudu() + "\r\n"

                    + BaseApplication.myCheckDataModel.getShijian() + "\r\n"

                    + BaseApplication.myCheckDataModel.getShijian() + "\r\n"

                    + BaseApplication.myCheckDataModel.getType0() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhoushu0() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhongliang0() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisuzhongxin00() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisuzhongxin01() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhongsuzhognxin00() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhongsuzhognxin01() + "\r\n"
                    + BaseApplication.myCheckDataModel.getGaosuzhognxin00() + "\r\n"
                    + BaseApplication.myCheckDataModel.getGaosuzhognxin01() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisupianzuo00() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisupianzuo01() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisupianyou00() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisupianyou01() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZuidawucha1() + "\r\n"
                    + BaseApplication.myCheckDataModel.getMPE1() + "\r\n"
                    + BaseApplication.myCheckDataModel.getHege1() + "\r\n"
                    + BaseApplication.myCheckDataModel.getType1() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhoushu1() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhongliang1() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisuzhongxin10() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisuzhongxin11() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhongsuzhognxin10() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZhongsuzhognxin11() + "\r\n"
                    + BaseApplication.myCheckDataModel.getGaosuzhognxin10() + "\r\n"
                    + BaseApplication.myCheckDataModel.getGaosuzhognxin11() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisupianzuo10() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisupianzuo11() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisupianyou10() + "\r\n"
                    + BaseApplication.myCheckDataModel.getDisupianyou11() + "\r\n"
                    + BaseApplication.myCheckDataModel.getZuidawucha2() + "\r\n"
                    + BaseApplication.myCheckDataModel.getMPE2() + "\r\n"
                    + BaseApplication.myCheckDataModel.getHege2() + "\r\n"
                    + BaseApplication.myCheckDataModel.getUname() + "\r\n"
            ;
        }

        PDALogUtils.createTxtFile(fileName, str.getBytes(), this);

        Toast.makeText(this, "生成报表成功", Toast.LENGTH_LONG).show();
    }

    private void showMyDialog(String[] str, final int id) {
        BottomMenu.show(this, str, new OnMenuItemClickListener() {
            @Override
            public void onClick(String text, int index) {
                //返回参数 text 即菜单名称，index 即菜单索引
                UtilsLog.e("---showMyDialog==" + text + "===" + index);
                switch (id) {
                    case R.id.ac_home_jianding_didian:
                        acHomeJiandingDidian.setText(text);
                        break;
                    case R.id.ac_home_zhizao_danwei:
                        acHomeZhizaoDanwei.setText(text);
                        break;
                }
            }
        }).setMenuTextInfo(new TextInfo().setBold(true).setFontColor(Color.BLACK).setGravity(Gravity.CENTER));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLocationClient != null) {
            mLocationClient.stop();
        }
        if (BaseApplication.myCheckDataModel != null) {
            BaseApplication.myCheckDataModel = new CheckDataModel();
        }
    }
}
